package mud;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class SimpleSocketImpl implements SimpleSocket {

    private final Socket socket;
    private final BufferedReader in;
    private final PrintWriter out;

    public SimpleSocketImpl(Socket socket) throws IOException {
        if (socket == null) {
            throw new IOException();
        }
        this.socket = socket;
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(socket.getOutputStream(), true);
    }

    @Override
    public String read() throws IOException {
        return in.readLine();
    }

    @Override
    public void write(String sendStr) throws IOException {
        if (socket.isClosed() || sendStr == null) {
            throw new IOException();
        }
        out.println(sendStr);
    }

}
